<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="js/jquery-3.2.1.min.js"></script>
    <style>
        * {
            margin: 0px;
            padding: 0px;
        }
        
        .box {
            width: 250px;
            height: 52px;
            margin: 20px auto;
            border: 2px solid #ccc;
            border-radius: 16px;
            padding: 15px 30px;
        }
        
        .box div {
            margin-left: 5px;
            float: left;
            width: 50px;
            height: 50px;
            border: 1px solid #666;
            border-radius: 50px;
        }
        
        .box .count {
            width: 60px;
            color: #666;
            font-size: 280%;
            line-height: 50px;
            padding-left: 6px;
            margin-left: 5px;
            border: 1px solid #fff;
        }
        
        .red {
            background-color: red;
        }
        
        .yellow {
            background-color: yellow;
        }
        
        .green {
            background-color: green;
        }
        
        .gray {
            background-color: #eee;
        }
    </style>
</head>

<body>
    <div class="box">
        <div id="red"></div>
        <div id="yellow"></div>
        <div id="green"></div>
        <div class="count" id="count">10</div>
    </div>
</body>
<script>
    var lamp = {
        red: {
            obj: $("#red"),
            timeout: 30,
            style: ['red', 'gray', 'gray'],
            next: 'green'
        },
        yellow: {
            obj: $("#yellow"),
            timeout: 5,
            style: ['gray', 'yellow', 'gray'],
            next: 'red'
        },
        green: {
            obj: $("#green"),
            timeout: 35,
            style: ['gray', 'gray', 'green'],
            next: 'yellow'
        },
        changeStyle(style) {
            $(this.red.obj).attr("class", style[0]);
            $(this.yellow.obj).attr("class", style[1]);
            $(this.green.obj).attr("class", style[2]);
        }
    }

    var count = {
        obj: $("#count"),
        change: function(num) {
            $(this.obj).html(num < 10 ? '0' + num : num);
        }
    }

    var now = lamp.red; //当前是绿灯
    var timeout = now.timeout;
    lamp.changeStyle(now.style);
    count.change(timeout);

    var id = setInterval(() => {
        if (--timeout <= 0) {
            now = lamp[now.next];
            timeout = now.timeout;
            lamp.changeStyle(now.style);
        }
        count.change(timeout);
    }, 1000);
</script>

</html>